﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Xml.Linq;
using System.Web.Services;
using Telerik.Web.UI;
using KPIS.GERP.GBMS.IBLL;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class BGM_Project_Work_Plan : System.Web.UI.Page
    {

        private IProject projectBLL;
        private ComboBLL comboBLL = new ComboBLL();
        private SystemTypeYearBLL sysTypeYearBLL = new SystemTypeYearBLL();
        private string type;
        string[] strMon = { "ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค." };
        string[] strMonth = { "มกราคม", "กุมภาพันธ์", "มีนาคม", "เมษายน", "พฤษภาคม", "มิถุนายน", "กรกฎาคม", "สิงหาคม", "กันยายน", "ตุลาคม", "พฤศจิกายน", "ธันวาคม" };

        protected void Page_Load(object sender, EventArgs e)
        {
            type = Request.QueryString["type"].ToString();

            if (type.Length > 0)
                projectBLL = Project_Factory.GetProjectFactory(type);

            Session["num"] = Session["num"] != null ? Session["num"] : Session["num"] = 1;
            createTarget_t5(int.Parse(Session["num"].ToString()));

            if (!IsPostBack)
            {
                if (!Object.Equals(Session["ProjectID"], null))
                {
                    lbProjSeq.Text = Session["ProjectID"].ToString();
                }

                LoadData();
            }
        }

        private void LoadData()
        {
            DataRow dr = sysTypeYearBLL.GetDataSystemTypeYear().Rows[0];
            lblMonthType_t5.Text = dr["month"].ToString();

            DataTable dt = projectBLL.GetDataProjectActivityPlan(lbProjSeq.Text);
            Grid_t5.DataSource = dt;
            Grid_t5.DataBind();

        }

        private void loadDataGridIn_t5()
        {
            DataTable dt = projectBLL.GetDataProjectActivityPlanTarget(lblSeq_t5.Text);
            RadGrid grdIn_t5 = (RadGrid)((GridDataItem)Grid_t5.Items[int.Parse(lblSeqIn_t5.Text)]).ChildItem.FindControl("GridIn_t5");
            grdIn_t5.DataSource = dt;
            grdIn_t5.DataBind();
        }

        int eventCount_createTarget_t5;

        private void createTarget_t5(int num)
        {
            for (int i = 1; i <= num; i++)
            {
                eventCount_createTarget_t5 = i;
                UserControl uc = new UserControl();
                uc = (UserControl)LoadControl("../Controls/Panel/TargetPanel.ascx");
                uc.ID = "target" + i.ToString();
                PanelFlexibleControl_t5.Controls.Add(uc);
                Label lblU = (Label)uc.FindControl("lblNum_U");
                if (lblItem_t5.Text != "0")     //edit
                {
                    PanelFlexibleControl_t5.Visible = true;
                    lblU.Text = (int.Parse(lblItem_t5.Text) + i).ToString();
                }
                else                            //add
                {
                    PanelFlexibleControl_t5.Visible = true;
                    lblU.Text = i.ToString();
                }
                //lblU.Text = i.ToString();

                UserControl cbmStartU = (UserControl)uc.FindControl("cmbMonthStart_U");
                RadComboBox cbmStart = (RadComboBox)cbmStartU.FindControl("month");
                UserControl cbmEndU = (UserControl)uc.FindControl("cmbMonthEnd_U");
                RadComboBox cbmEnd = (RadComboBox)cbmEndU.FindControl("month");
                cbmStart.SelectedValue = lblMonthType_t5.Text;
                cbmEnd.SelectedValue = lblMonthType_t5.Text;

            }
        }
        private void iniGridHeaderMonth(GridHeaderItem header)
        {
            int monType = int.Parse(lblMonthType_t5.Text);
            int tmp;
            string strColumn;
            for (int i = 0; i < 12; i++)
            {
                tmp = i + monType;
                if (tmp > 12)
                    tmp = tmp - 12;
                strColumn = "act_month" + (i + 1).ToString();
                header[strColumn].Text = strMon[tmp - 1];
                Label lbl = (Label)PanelAddEdit_t5.FindControl("lblMonth" + (i + 1).ToString());
                lbl.Text = strMonth[tmp - 1] + " : ";
            }
        }

        private void clear_t5()
        {
            txtDescr_t5.Text = "";
            PanelFlexibleControl_t5.Visible = true;
            lblItem_t5.Text = "0";


            foreach (Control obj in this.PanelAddEdit_t5.Controls)
            {
                if (obj is CheckBox)
                {
                    CheckBox cb = (CheckBox)obj;
                    cb.Checked = false;
                }
            }

            Session["num"] = 1;

            //foreach (Control obj in this.PanelFlexibleControl_t5.Controls)
            //{
            //    if (obj is UserControl)
            //    {
            //        UserControl uc = (UserControl)obj;
            //        PanelFlexibleControl_t5.Controls.Remove(uc);
            //    }
            //}
            this.PanelFlexibleControl_t5.Controls.Clear();
        }

        private void bindTab5_forEdit(string seq)
        {
            DataRow dr = projectBLL.GetDataProjectActivityPlanBySequence(seq).Rows[0];
            txtDescr_t5.Text = dr["descr"].ToString();
            chkMonth1.Checked = dr["act_month1"].ToString() == "1" ? true : false;
            chkMonth2.Checked = dr["act_month2"].ToString() == "1" ? true : false;
            chkMonth3.Checked = dr["act_month3"].ToString() == "1" ? true : false;
            chkMonth4.Checked = dr["act_month4"].ToString() == "1" ? true : false;
            chkMonth5.Checked = dr["act_month5"].ToString() == "1" ? true : false;
            chkMonth6.Checked = dr["act_month6"].ToString() == "1" ? true : false;
            chkMonth7.Checked = dr["act_month7"].ToString() == "1" ? true : false;
            chkMonth8.Checked = dr["act_month8"].ToString() == "1" ? true : false;
            chkMonth9.Checked = dr["act_month9"].ToString() == "1" ? true : false;
            chkMonth10.Checked = dr["act_month10"].ToString() == "1" ? true : false;
            chkMonth11.Checked = dr["act_month11"].ToString() == "1" ? true : false;
            chkMonth12.Checked = dr["act_month12"].ToString() == "1" ? true : false;
            lblItem_t5.Text = dr["rowTarget"].ToString();
        }


        //---- event control

        protected void Grid_t5_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "TargetInsertClick")
            {
                PanelAddEdit_t5.Visible = true;
                clear_t5();
                PanelFlexibleControl_t5.Visible = false;
                btnSave_t5.Visible = true;
                btnEdit_t5.Visible = false;
            }
            else if (e.CommandName == RadGrid.EditCommandName)
            {
                PanelAddEdit_t5.Visible = true;
                clear_t5();
                PanelFlexibleControl_t5.Visible = false;
                btnSave_t5.Visible = false;
                btnEdit_t5.Visible = true;
                lblSeq_t5.Text = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"].ToString();
                bindTab5_forEdit(lblSeq_t5.Text);
            }
            else if (e.CommandName == "Delete")
            {
                int nRowSelected = 0;
                foreach (GridDataItem data in Grid_t5.MasterTableView.GetSelectedItems())
                {
                    projectBLL.DeleteProjectActivityPlan(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Sequence"].ToString());
                    nRowSelected++;
                }
                clear_t5();
                PanelAddEdit_t5.Visible = false;
                //  Utils.Web.ShowAlert(this.RadPageView5, "ลบแล้ว " + nRowSelected.ToString() + "รายการ");
            }
            else if (e.CommandName == RadGrid.ExpandCollapseCommandName)
            {
                lblSeq_t5.Text = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"].ToString();
                lblSeqIn_t5.Text = e.Item.ItemIndex.ToString();
                loadDataGridIn_t5();
                GridHeaderItem header = (GridHeaderItem)Grid_t5.MasterTableView.GetItems(GridItemType.Header)[0];
                iniGridHeaderMonth(header);
                return;
            }
            else
            {
            }
            LoadData();
        }

        protected void Grid_t5_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridHeaderItem)
            {
                GridHeaderItem header = (GridHeaderItem)e.Item;
                iniGridHeaderMonth(header);
            }

            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                for (int i = 0; i < 12; i++)
                {
                    if (e.Item.Cells[i + 5].Text == "1")
                        e.Item.Cells[i + 5].BackColor = System.Drawing.Color.Red; //.ColorTranslator.FromHtml("#D8DFEA");
                    e.Item.Cells[i + 5].Text = "";
                }
            }
        }

        protected void GridIn_t5_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                var dataItem = e.Item as GridDataItem;

                dataItem["start_month"].Text = strMonth[Convert.ToInt32(dataItem["start_month"].Text) - 1];
                dataItem["end_month"].Text = strMonth[Convert.ToInt32(dataItem["end_month"].Text) - 1];
            }

        }

        protected void GridIn_t5_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                RadGrid grdIn_t5 = (RadGrid)((GridDataItem)Grid_t5.Items[int.Parse(lblSeqIn_t5.Text)]).ChildItem.FindControl("GridIn_t5");
                int nRowSelected = 0;

                foreach (GridDataItem data in grdIn_t5.MasterTableView.GetSelectedItems())
                {
                    projectBLL.DeleteProjectActivityPlanTarget(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Sequence"].ToString());
                    nRowSelected++;
                }

                loadDataGridIn_t5();
            }

        }

        protected void btnAddControl_t5_Click(object sender, EventArgs e)
        {
            UserControl uc = new UserControl();
            uc = (UserControl)PanelFlexibleControl_t5.FindControl("target" + eventCount_createTarget_t5.ToString());
            if (eventCount_createTarget_t5 == 1)
            {
                RadTextBox txtTarget = (RadTextBox)uc.FindControl("txtTarget_U");
                txtTarget.Text = "";
            }

            UserControl cbmStartU = (UserControl)uc.FindControl("cmbMonthStart_U");
            RadComboBox cbmStart = (RadComboBox)cbmStartU.FindControl("month");
            UserControl cbmEndU = (UserControl)uc.FindControl("cmbMonthEnd_U");
            RadComboBox cbmEnd = (RadComboBox)cbmEndU.FindControl("month");
            cbmStart.SelectedValue = lblMonthType_t5.Text;
            cbmEnd.SelectedValue = lblMonthType_t5.Text;
            Session["num"] = int.Parse(Session["num"].ToString()) + 1;
        }

        protected void btnCancel_t5_Click(object sender, EventArgs e)
        {
            clear_t5();
            PanelAddEdit_t5.Visible = false;
        }

        protected void btnSave_t5_Click(object sender, EventArgs e)
        {
            int reqAP_seq = projectBLL.InsertProjectActivityPlan(lbProjSeq.Text, txtDescr_t5.Text
                , chkMonth1.Checked == true ? 1 : 0, chkMonth2.Checked == true ? 1 : 0
                , chkMonth3.Checked == true ? 1 : 0, chkMonth4.Checked == true ? 1 : 0
                , chkMonth5.Checked == true ? 1 : 0, chkMonth6.Checked == true ? 1 : 0
                , chkMonth7.Checked == true ? 1 : 0, chkMonth8.Checked == true ? 1 : 0
                , chkMonth9.Checked == true ? 1 : 0, chkMonth10.Checked == true ? 1 : 0
                , chkMonth11.Checked == true ? 1 : 0, chkMonth12.Checked == true ? 1 : 0
                , new SsMng().GetUss(SsMng.SsUserType.EmpSeq));


            for (int i = 1; i <= int.Parse(Session["num"].ToString()) - 1; i++)
            {
                UserControl uc = new UserControl();
                uc = (UserControl)PanelFlexibleControl_t5.FindControl("target" + i.ToString());
                RadTextBox txtTarget = (RadTextBox)uc.FindControl("txtTarget_U");

                UserControl cbmStartU = (UserControl)uc.FindControl("cmbMonthStart_U");
                RadComboBox cbmStart = (RadComboBox)cbmStartU.FindControl("month");
                UserControl cbmEndU = (UserControl)uc.FindControl("cmbMonthEnd_U");
                RadComboBox cbmEnd = (RadComboBox)cbmEndU.FindControl("month");
                projectBLL.InsertProjectActivityPlanTarget(reqAP_seq.ToString(), txtTarget.Text
                    , cbmStart.SelectedValue, cbmEnd.SelectedValue, new SsMng().GetUss(SsMng.SsUserType.EmpSeq));
            }
            LoadData();
            clear_t5();
            PanelAddEdit_t5.Visible = false;
        }

        protected void btnEdit_t5_Click(object sender, EventArgs e)
        {
            projectBLL.UpdateProjectActivityPlan(lblSeq_t5.Text, txtDescr_t5.Text
                , chkMonth1.Checked == true ? 1 : 0, chkMonth2.Checked == true ? 1 : 0
                , chkMonth3.Checked == true ? 1 : 0, chkMonth4.Checked == true ? 1 : 0
                , chkMonth5.Checked == true ? 1 : 0, chkMonth6.Checked == true ? 1 : 0
                , chkMonth7.Checked == true ? 1 : 0, chkMonth8.Checked == true ? 1 : 0
                , chkMonth9.Checked == true ? 1 : 0, chkMonth10.Checked == true ? 1 : 0
                , chkMonth11.Checked == true ? 1 : 0, chkMonth12.Checked == true ? 1 : 0
                , new SsMng().GetUss(SsMng.SsUserType.EmpSeq));

            for (int i = 1; i < int.Parse(Session["num"].ToString()); i++)
            {
                UserControl uc = new UserControl();
                uc = (UserControl)PanelFlexibleControl_t5.FindControl("target" + i.ToString());
                RadTextBox txtTarget = (RadTextBox)uc.FindControl("txtTarget_U");

                UserControl cbmStartU = (UserControl)uc.FindControl("cmbMonthStart_U");
                RadComboBox cbmStart = (RadComboBox)cbmStartU.FindControl("month");
                UserControl cbmEndU = (UserControl)uc.FindControl("cmbMonthEnd_U");
                RadComboBox cbmEnd = (RadComboBox)cbmEndU.FindControl("month");
                projectBLL.InsertProjectActivityPlanTarget(lblSeq_t5.Text, txtTarget.Text
                    , cbmStart.SelectedValue, cbmEnd.SelectedValue, new SsMng().GetUss(SsMng.SsUserType.EmpSeq));
            }
            LoadData();
            clear_t5();
            PanelAddEdit_t5.Visible = false;
        }

        protected void Grid_t5_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridCommandItem)
            {
                GridCommandItem commandItem = e.Item as GridCommandItem;
                LinkButton button = commandItem.FindControl("AddNew_t5") as LinkButton;
                LinkButton buttonDel = commandItem.FindControl("Delete_t5") as LinkButton;
                if ((bool)Session["Approve"])
                {
                    button.Enabled = false;
                    button.Visible = true;
                    button.Text = "";

                    buttonDel.Enabled = false;
                    buttonDel.Visible = true;
                    buttonDel.Text = "";
                }
            }
        }
    }
}
